<?php
namespace frontend\controllers;
use bc\controller\WxOAuthController;
use bc\model\wc\DoctorModel;
use bc\model\wx\report\CodeModel;
use bc\util\Sms;
use bc\wx\Auth;
use bc\wx\User as User;
use bc\model\wx\User as wxUser;
use yii\web\Cookie;
use bc\util\Util;
use yii\web\Session;

class DoctorController extends WxOAuthController
{
	public $userInfop=0;
	public $attention=0;
	public $openid=0;
	/*
	 * 初始化
	 * */
	public function init()
	{
		parent::init(); // TODO: Change the autogenerated stub
		$session = new Session();
		$user = $session->get('user');
		$this->openid = $user ['openid'];
	}

	/*
	* 获取用户基本信息
	* */

	public function actionLogin()
	{
		return $this->render('login');
	}

	public function actionInter()
	{
		$doctorModel = new DoctorModel();
		$mobile = $this->post('mobile');
		$info = $doctorModel->getRow(['phone'=>$mobile]);
		if($info){
			//设置COOKIE
			$rspCookies = $this->response()->cookies;
			$rspCookies->add(new \yii\web\Cookie([
				'name' => 'doctor',
				'value' => $info['id'],
				'expire'=>time()+3600*12,
			]));
			$this->redirect('/doctor/index');
		}else{
			echo "<script>alert('您还不是注册医生');window.location.href='/doctor/login';</script>";
			die();
		}
	}
	public function actionIndex()
	{
		//读取Cookie
		$reqCookies = $this->getRequest()->cookies;
		$doctorCookie = $reqCookies->get('doctor');
		if( !$doctorCookie ){
			$this->redirect('/doctor/login');die();
		}
		//更新当前用户信息
		$user = new User();
		$appid = $this->wechat()->appid;
		$secret = $this->wechat()->secret;
		$auth = new Auth($appid, $secret);
		$accessToken = $auth->accessToken;
		$info = $user->getUserInfo($accessToken, $this->openid);
		if(!empty($info))
		{
			$data = array(
				'nickname' => Util::replace_emoji($info['nickname']),
				'headimgurl' => $info['headimgurl']
			);
			$where =  array(
				'openid'=>$this->openid
			);
			$wxUserM = new wxUser();
			$wxUserR = $wxUserM->getRow($where);
			$wxUserM->modify($data, $wxUserR['$this->openid']);
		}
		//获取当前用户是不是医生
		$doctorM = new DoctorModel();
		$userInfoc=$doctorM->getDoctorFromOpenid($this->openid);
		if(empty($userInfoc)){
			$url='http://wx.zhishimama.com/shop/mobile/user.php';
			$this->redirect($url);
		}
		$row ['userInfoc'] = $userInfoc;
		//var_dump($row['userInfoc']);die;
		return $this->render('index', $row);
	}

	public function actionList()
	{
		$reqCookies = $this->getRequest()->cookies;
		$doctorCookie = $reqCookies->get('doctor');

		if( !$doctorCookie ){
			$this->redirect('/doctor/login');die();
		}

		//判断是不是医生
		$doctorModel= new DoctorModel();
		$doctorModel->columns='bc_wc_doctor.id as d_id,user_id,hospital_id,phone,bc_wc_hospital.id as h_id,name,address,bc_user.id as uid,real_name,nick_name,wxid';
		$doctorData=$doctorModel->getDoctorList(array('bc_user.wxid'=>$this->openid));
		if(empty($doctorData)){
			$this->redirect('/doctor/login');
		}
		//获取医生的信息
		if(empty($size=$_GET['size']))
			$size=0;
		$doctorList = $doctorModel->getPromotelist($size,$doctorData[0]['d_id']);
		$status1=0;
		$status2=0;
		$status3=0;
		$status4=0;
		foreach($doctorList['data'] as $k => $v)
		{
			if($v['status'] === '1' )
			{
				$status1++;
			}elseif($v['status'] === '2' ){
				$status2++;
			}elseif($v['status'] === '3' ){
				$status3++;
			}elseif($v['status'] === '4' ){
				$status4++;
			}
		}
		$row['year']=$doctorList['tmp_year'];
		$row['mon']=$doctorList['tmp_mon'];
		$row['status1']=$status1;
		$row['status2']=$status2;
		$row['status3']=$status3;
		$row['status4']=$status4;
		$row['size_s']=$size-1;
		$row['size_x']=$size+1;

		return $this->render('list',$row);
	}


	/**
	 * 校验验证码
	 */
	public function actionValidcode()
	{
		$msg ['error'] = 1;

		if ($this->getRequest ()->isAjax == true)
		{
			$mobile = $this->get ( 'mobile' );
			$code = $this->get ( 'code' );
			$params ['mobile'] = $mobile;

			$codeModel = new CodeModel ();
			$codeModel->sort = 'id DESC';
			$code_row = $codeModel->getList ( [
				'mobile' => $mobile,
				'code' => $code
			] );
			$msg ['data'] = json_encode ( $code_row );
			if (! $code_row [0])
			{
				$msg ['msg'] = '请输入正确的验证码';
			}
			elseif ($code_row [0] ['create_time'] < time () - 60)
			{
				$msg ['msg'] = '验证码已过期';
			}
			else
			{
				$msg ['error'] = 0;
			}
		}
		else
		{
			$msg ['msg'] = '请求异常';
		}

		echo json_encode ( $msg );
		exit ();
	}

	/**
	 * ajax方式发送验证码
	 */
	public function actionSend()
	{
		$msg = [ ];

		if ($this->getRequest ()->isAjax == true)
		{
			$mobile = $this->get ( 'mobile' );
			$model = new \bc\model\wx\ReportModel ();
			$params ['mobile'] = $mobile;
			// 13651886355

			// 创建一个新的验证码
			$arr = [ ];

			for($i = 0; $i < 4; $i ++)
			{
				$rand = rand ( 0, 9 );
				$arr [] = $rand;
			}

			$code = implode ( '', $arr );
			$data ['code'] = $code;
			$data ['mobile'] = $mobile;
			$data ['create_time'] = time ();

			$codeModel = new CodeModel ();
			$codeModel->sort = 'id DESC';
			$code_row = $codeModel->getList ( [
				'mobile' => $mobile
			] );
			$codeModel->add ( $data );

			// 发送短信验证码
			$sms_msg = '验证码' . $code . '，本次验证码60s内有效';

			$sms = new Sms ();
			$sms->setMobile ( $mobile );
			$sms->setText ( $sms_msg );
			$result = $sms->send ();
			$msg ['error'] = 0;
			$msg ['result'] = $result;
			$msg ['msg'] = '验证码已发送!';
		}
		else
		{
			$msg ['error'] = 1;
			$msg ['msg'] = '请求参数错误！';
		}

		echo json_encode ( $msg );
		exit ();
	}
}
